import { getGameObjectById } from "../engine";
import { Behaviour } from "../engine/Behaviour";
import { Transform } from "../engine/Transform";
import { number } from "../engine/validators/number";
import { AnimationBehaviour } from "./AnimationBehaviour";
import { RigidBody } from "./RigidBody";
import { Sound } from "./sound";


//用于相机的时候要写一个左右距离的限制
//注意 所有场景的长度（或者说图片拼接数量）要一致



export class Walkable extends Behaviour {



    @number()
    speed = 1;


    onStart() {
        const rigid = this.gameObject.getBehaviour(RigidBody);

        window.addEventListener('keydown', (e) => {
            if(getGameObjectById('PauseBackGround')===undefined){
                if(e.key==='a'||e.key==='A'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    
                    animationBehaviour.action='leftrun'
                    rigid.b2RigidBody.SetLinearVelocity({ x: -this.speed, y: 0 })
                }
                if(e.key==='d'||e.key==='D'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    animationBehaviour.action='rightrun'
                    rigid.b2RigidBody.SetLinearVelocity({ x: this.speed, y: 0 })
                }
            }


            else{
            if(getGameObjectById('PauseBackGround').active===true){


            }
            else{
                if(e.key==='a'||e.key==='A'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    
                    animationBehaviour.action='leftrun'
                    rigid.b2RigidBody.SetLinearVelocity({ x: -this.speed, y: 0 })
                }
                if(e.key==='d'||e.key==='D'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    animationBehaviour.action='rightrun'
                    rigid.b2RigidBody.SetLinearVelocity({ x: this.speed, y: 0 })
                }
            }}


         })

         
        window.addEventListener('keyup', (e) => {
            if(getGameObjectById('PauseBackGround')===undefined){

                if(e.key==='a'||e.key==='A'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    animationBehaviour.action='leftleisure'
                    rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: 0 })
                }
                if(e.key==='d'||e.key==='D'){
                    const ani=getGameObjectById('Alice');
                    const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                    animationBehaviour.action='rightleisure'
                    rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: 0 })
                }
            }
            else{

                if(getGameObjectById('PauseBackGround').active===true){

                }
                    else{
                    if(e.key==='a'||e.key==='A'){
                        const ani=getGameObjectById('Alice');
                        const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                        animationBehaviour.action='leftleisure'
                        rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: 0 })
                    }
                    if(e.key==='d'||e.key==='D'){
                        const ani=getGameObjectById('Alice');
                        const animationBehaviour=ani.getBehaviour(AnimationBehaviour);
                        animationBehaviour.action='rightleisure'
                        rigid.b2RigidBody.SetLinearVelocity({ x: 0, y: 0 })
                    }
                    }
            }


         })
    }

    onUpdate(): void {
    }
}
